package com.hj.security.service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.android.internal.telephony.ITelephony;
import com.hj.security.R;
import com.hj.security.dao.BlackNumberDao;
import com.hj.security.engine.NumberAddressService;
import com.hj.security.ui.NumberSecurityActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.database.Cursor;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.provider.CallLog;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AddressService extends Service {

	private TelephonyManager telephonyManager;
	private MyPhoneListener listener;
	private WindowManager windowManager;
	private TextView tv;
	private View view;
	private BlackNumberDao dao;
	private long start;
	private long end;
	
	private SharedPreferences sp;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void onCreate() {
		System.out.println("AddressService创建");
		super.onCreate();
		listener = new MyPhoneListener();
		windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
		
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		dao = new BlackNumberDao(this);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
	}

	/**
	 * 挂断电话
	 */
	private void endCall(){
		try {
			//通过反射拿到android.os.ServiceManager里面的getService这个方法的对象
			Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
			IBinder iBinder = (IBinder) method.invoke(null, new Object[]{TELEPHONY_SERVICE});
			//通过反射调用这个getService方法，然后拿到IBinder对象，然后就可以进行aidl啦
			ITelephony telephony = ITelephony.Stub.asInterface(iBinder);
			telephony.endCall();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	private void cleanCallLog(String incomingNumber){
		ContentResolver resolver = getContentResolver();
		Cursor cursor = resolver.query(CallLog.Calls.CONTENT_URI, null, "number = ?", new String[]{incomingNumber}, null);
		if(cursor.moveToNext()){
			String id = cursor.getString(cursor.getColumnIndex("_id"));
			resolver.delete(CallLog.Calls.CONTENT_URI, "_id = ?", new String[]{incomingNumber});
		}
	}
	/**
	 * 显示归属地的窗体
	 * @param address
	 */
	private void showLocation(String address){
		LayoutParams params = new LayoutParams();
		params.width = LayoutParams.WRAP_CONTENT;
		params.height = LayoutParams.WRAP_CONTENT;
		// |是或运算
		params.flags = LayoutParams.FLAG_NOT_FOCUSABLE //无法获取
				|LayoutParams.FLAG_NOT_TOUCHABLE //无法点击
				|LayoutParams.FLAG_KEEP_SCREEN_ON;//保持屏幕亮
		params.format = PixelFormat.TRANSLUCENT;
		params.type = WindowManager.LayoutParams.TYPE_TOAST;
		params.setTitle("Toast");
		
		//主要是确定坐标系是从左上角开始的，不然待会设置位置的时候有些麻烦
		params.gravity = Gravity.LEFT | Gravity.TOP;
		params.x = sp.getInt("lastX", 0);
		params.y = sp.getInt("lastY", 0);
		
		view = View.inflate(this, R.layout.show_location, null);
		LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll_location);
		int type = sp.getInt("background", 0);
		
		switch (type) {
		case 0:
			ll.setBackgroundResource(R.drawable.call_locate_white);
			break;
		case 1:
			ll.setBackgroundResource(R.drawable.call_locate_orange);
			break;
		case 2:
			ll.setBackgroundResource(R.drawable.call_locate_green);
			break;
		case 3:
			ll.setBackgroundResource(R.drawable.call_locate_blue);
			break;
		case 4:
			ll.setBackgroundResource(R.drawable.call_locate_gray);
			break;

		default:
			break;
		}
		
		tv = (TextView) view.findViewById(R.id.tv_show_location);
		tv.setText("归属地："+address);
		windowManager.addView(view, params);
	}
	private void showNotifycation(String number){
		//拿到Notification的管理者
		NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		//new一个Nofitication出来
		Notification notification = new Notification(R.drawable.notification, "发现响一声电话", System.currentTimeMillis());
		Context context = getApplicationContext();
		//设置成一点击就消失  
		notification.flags = Notification.FLAG_AUTO_CANCEL;
		Intent notificationIntent = new Intent(context, NumberSecurityActivity.class);
		notificationIntent.putExtra("number", number);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
		notification.setLatestEventInfo(context, "响一声号码", number, pendingIntent);
		//激活Nofitication
		notificationManager.notify(0, notification);
	}
	private class MyPhoneListener extends PhoneStateListener{
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			super.onCallStateChanged(state, incomingNumber);
			switch (state) {
			case TelephonyManager.CALL_STATE_IDLE://空闲状态
				end = System.currentTimeMillis();
				if(end>start && end-start>2000){
					showNotifycation(incomingNumber);
				}
				if(tv != null){
					windowManager.removeView(tv);
				}
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK://接通电话
				System.out.println("接通");
				if(tv != null){
					windowManager.removeView(tv);
				}			
				break;
			case TelephonyManager.CALL_STATE_RINGING://铃响状态
				start = System.currentTimeMillis();
				if(dao.find(incomingNumber)){
					endCall();
					getContentResolver().registerContentObserver(CallLog.Calls.CONTENT_URI, true, new MyObserver(new Handler(), incomingNumber));
				}
				String address = NumberAddressService.getAddress(incomingNumber);
				showLocation(address);
				break;

			default:
				break;
			}
		}
	}
	private class MyObserver extends ContentObserver{

		private String number;
		public MyObserver(Handler handler, String number) {
			super(handler);
			this.number = number;
		}
		@Override
		public void onChange(boolean selfChange) {
			super.onChange(selfChange);
			cleanCallLog(number);
			getContentResolver().unregisterContentObserver(this);
		}
	}
}
